<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/26
 * Time: 7:13
 */
namespace app\admin\controller;
use app\admin\model\Mokuais;
use think\Controller;
use think\Request;

class Mokuai extends Controller
{
    /**加载表格
     * @param Request $request
     * @return mixed
     */
    public function show_list(Request $request){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('mokuai/del')
            ->addTableHeaderAdd('mokuai/show_add')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text','模块名称')
            ->addTableColumn('status','switch','状态',['开启','禁用'],'mokuai/change')
            ->addTableRightHeader('操作')
            ->addTableRight('edit','mokuai/show_edit','修改')
            ->addTableRight('del','mokuai/del','删除')
            ->addTablePage('mokuai/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $data = Mokuais::where('id','>',0)
            ->paginate($limit)
            ->toArray();
        return json_page_response($data);
    }

    /**修改数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function change(Request $request){
        $id = $request->param('id');
        $data = Mokuais::where('id',$id)->findOrEmpty()->toArray();
        if($data['status']=='1'){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Mokuais::where('id',$data['id'])->update(['status'=>$data['status']])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**加载添加
     * @param Request $request
     * @return mixed
     */
    public function show_add(Request $request){
        return NewBuilder::make('form')
            ->addAction('mokuai/add')
            ->addInput('name','模块名称')
            ->addSwitch('status','模块状态',1)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(Mokuais::create($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    /**加载修改
     * @param Request $request
     * @return mixed
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Mokuais::where('id',$id)->findOrEmpty()->toArray();
        return NewBuilder::make('form')
            ->addAction('mokuai/edit')
            ->addNone('id',$id)
            ->addInput('name','模块名称',$data['name'])
            ->addSwitch('status','模块状态',$data['status'])
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**编辑
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(Mokuais::update($data,['id'=>$data['id']])){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除
     * @param Request $request
     * @return \think\response\Json
     */
    public function del(Request $request){
        $id = $request->param('id');
        if(Mokuais::where('id','in',$id)->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}